package com.itheima.web.controller.system;

import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.itheima.core.BaseServlet;
import com.itheima.core.ResponseBody;
import com.itheima.domain.system.Role;
import com.itheima.service.system.ModuleService;
import com.itheima.service.system.RoleService;
import com.itheima.service.system.impl.ModuleServiceImpl;
import com.itheima.service.system.impl.RoleServiceImpl;
import com.itheima.utils.BeanUtil;
import com.sun.org.apache.regexp.internal.RE;
import org.apache.ibatis.annotations.Insert;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@WebServlet("/system/role/*")
public class RoleServlet extends BaseServlet {
    private RoleService roleService = new RoleServiceImpl();
    private ModuleService moduleService = new ModuleServiceImpl();

    /**
     * 分页查询
     */
    public String list(HttpServletRequest request, HttpServletResponse response) throws Exception{
        /*1.获取表单数据*/
        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("pageSize");

        if(StringUtil.isEmpty(pageNum)){
            pageNum = "1";
        }
        if(StringUtil.isEmpty(pageSize)){
            pageSize = "5";
        }

        PageInfo<Role> pageInfo = roleService.queryPage(Integer.parseInt(pageNum), Integer.parseInt(pageSize));

        request.setAttribute("page",pageInfo);

        return "/WEB-INF/pages/system/role/list.jsp";
    }
    /**跳转到编辑页面*/
    public String toEdit(HttpServletRequest request, HttpServletResponse response) throws Exception{
        String id = request.getParameter("id");
        Role role = roleService.findById(id);

        request.setAttribute("role",role);
        return "/WEB-INF/pages/system/role/update.jsp";

    }
    /**编辑*/
    public String edit(HttpServletRequest request, HttpServletResponse response) throws Exception{
        Role r = BeanUtil.fillBean(request, Role.class, "yyyy-MM-dd");
        roleService.update(r);

        return  "/system/role/list";
    }

    /**跳转到新增页面*/
    public String toAdd(HttpServletRequest request, HttpServletResponse response) throws Exception{
        return  "/WEB-INF/pages/system/role/add.jsp";
    }
    /**新增*/
    public String save(HttpServletRequest request, HttpServletResponse response) throws Exception{
        Role r = BeanUtil.fillBean(request, Role.class, "yyyy-MM-dd");
        roleService.save(r);

        return  "/system/role/list";
    }

    /**跳转到新增页面*/
    public String author(HttpServletRequest request, HttpServletResponse response) throws Exception{
        String roleId = request.getParameter("id");

        Role role = roleService.findById(roleId);
        request.setAttribute("role",role);
        return  "/WEB-INF/pages/system/role/author.jsp";
    }


    @ResponseBody
    public Object findAuthorByRoleId(HttpServletRequest request, HttpServletResponse response) throws Exception{
        /*1.获取表单数据*/
        String roleId = request.getParameter("roleId");

        /*2.调用servce*/
        List<Map> list = moduleService.findAuthorByRoleId(roleId);

        /*3.返回数据*/
        return list;
    }


    @ResponseBody
    public Object authorize(HttpServletRequest request, HttpServletResponse response){
        try {
            String roleId = request.getParameter("roleId");
            String[] moduleIds = request.getParameter("moduleIds").split(",");//1,2,3,4,4

            roleService.updateAuthor(roleId,moduleIds);

            return "true";
        } catch (Exception e) {
            e.printStackTrace();
            return "false";
        }

    }


}
